<template>
    <van-cell-group>
        <van-cell icon="clock-o">
            <div slot="title">
                <div class="van-cell-text" style="white-space: nowrap">{{xiangmu}}考试公告预约</div>
                <div style="font-size: 12px; opacity: 0.4;">公告发布后将以短信形式通知</div>
            </div>
            <div>
                <div v-if="!dingyue"><van-button size="small" type="danger" @click="dyskgg">一键预约</van-button></div>
                <div v-else><van-button size="small" disabled type="danger" plain>已订阅</van-button></div>
            </div>
        </van-cell>
    </van-cell-group>
</template>

<script>
    export default {
        props:{
            xiangmu:{
                type: String,
                default: ''
            },
        },
        name: "dingyue",
        data () {
            return {
                dingyue: false,
                hasdyksxm: '',
            }
        },
        created() {
            this.chechdingyue()
        },
        computed: {
            userdata(){
                return this.$store.getters.userdata ? this.$store.getters.userdata : '';
            },
        },
        methods: {
            dyskgg(){
                var that = this
                if(!that.userdata){
                    that.$dialog.confirm({
                        title: '提示', message: '请先登录', confirmButtonText: '前往登陆', cancelButtonText: '取消'
                    }).then(() => {this.$router.push({ path: '/user/login' });}).catch(() => {});return
                }
                that.$toast.loading()
                that.$http.jsonp('https://zg99.offcn.com/index/chaxun/register/?actid=1456&callback=?', {
                    params: {
                        name: that.userdata.name,
                        phone: that.userdata.phone,
                        ksxm: that.hasdyksxm + ',' + that.xiangmu,
                    }
                })
                .then(function (res) {
                    var resjson = res.body
                    if(resjson.status=="1"){
                        that.$toast.success('订阅成功')
                        that.dingyue = true
                    }else{
                        that.dingyue = false
                        that.$toast.clear()
                    }
                })
            },
            chechdingyue(){
                var that = this;
                if(!that.userdata){ return }
                that.$http.jsonp('https://zg99.offcn.com/index/chaxun/getphonestatus/?actid=1456&callback=?', {
                    params: {
                        name: that.userdata.name,
                        phone: that.userdata.phone,
                    }
                })
                .then(function (res) {
                    var resjson = res.body
                    if(resjson.status=="1"){
                        that.hasdyksxm = resjson.user.ksxm
                        var ksxmarr = resjson.user.ksxm.split(',')
                        if(ksxmarr.indexOf(that.xiangmu) == -1){
                            that.dingyue = false
                        }else{
                            that.dingyue = true
                        }
                    }else{
                        that.dingyue = false
                    }
                })
            },
        }
    };
</script>

<style scoped>

</style>